// NewTrainingFramework.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "../Utilities/utilities.h" // if you use STL, please include this line AFTER all other include
#include "Vertex.h"
#include "Shaders.h"
#include <conio.h>
#include "Globals.h"
#include "Triangle.h"
#include "BaseCamera.h"


GLuint vboId;
Shaders myShaders;
Vertex verticesData[6];
Triangle triangle;
Triangle triangle2;

int Init ( ESContext *esContext )
{
	//Setup perspective	
	GLfloat aspect = (GLfloat)Globals::screenWidth / (GLfloat)Globals::screenHeight;
	BaseCamera::Projection.SetPerspective(45.0f, aspect, 1.0f, 1000.0f);

	triangle = Triangle ();
	triangle.SetPosition (
		Vector3(0.0f, 0.5f, 0.0f),
		Vector3(-0.5f, 0.0f, 0.0f),
		Vector3(0.5f, 0.0f, 0.0f));
	triangle.SetColor (
		Vector4(1.0f, 0.0f, 0.0f, 1.0f),
		Vector4(0.0f, 1.0f, 0.0f, 1.0f),
		Vector4(0.0f, 0.0f, 1.0f, 1.0f));
	triangle.Init ();

	triangle2 = Triangle();
	triangle2.SetPosition (
		Vector3(0.0f, 0.5f, 0.0f),
		Vector3(-0.5f, 0.0f, 0.0f),
		Vector3(0.5f, 0.0f, 0.0f));
	triangle2.SetColor (
		Vector4(1.0f, 0.0f, 0.0f, 1.0f),
		Vector4(0.0f, 1.0f, 0.0f, 1.0f),
		Vector4(0.0f, 0.0f, 1.0f, 1.0f));
	triangle2.Init();

	return myShaders.Init("../Resources/Shaders/TriangleShaderVS.vs", "../Resources/Shaders/TriangleShaderFS.fs");

}

void Update ( ESContext *esContext, float deltaTime )
{
	Globals::mainCamera->Update(esContext, deltaTime);
	//triangle.RotY += 0.001f;
	triangle.Update(esContext, deltaTime);
	//triangle2.RotY += -0.001f;
	triangle2.Translate = Vector3(1.0f, 0.0f, 2.0f);
	triangle2.ScaleX = 0.5f;
	triangle2.Update(esContext, deltaTime);
}


void Draw ( ESContext *esContext )
{
	glClear(GL_COLOR_BUFFER_BIT);

	glUseProgram(myShaders.program);

	triangle.Draw(esContext, &myShaders);
	triangle2.Draw(esContext, &myShaders);

	eglSwapBuffers(esContext->eglDisplay, esContext->eglSurface);
}

void Key ( ESContext *esContext, unsigned char key, bool bIsPressed)
{
	GLfloat offset = 0.01f;
	switch(key)
	{
	case 'S':
		{
			Globals::mainCamera->MoveUp(offset);
		}
		break;
	case 'W':
		{
			Globals::mainCamera->MoveDown(offset);
		}
		break;
	case 'A':
		{
			Globals::mainCamera->MoveLeft(offset);
		}
		break;
	case 'D':
		{
			Globals::mainCamera->MoveRight(offset);
		}
		break;
	case 'Z':
		{
			Globals::mainCamera->MoveForward(offset + 0.5f);
		}
		break;
	case 'X':
		{
			Globals::mainCamera->MoveBackward(offset + 0.5f);
		}
		break;
	case 'V':
		{
			Globals::mainCamera->RotateLeft(0.1f);
		}
		break;
	case 'B':
		{
			Globals::mainCamera->RotateRight(0.1f);
		}
		break;
	case 'G':
		{
			Globals::mainCamera->RotateDown(0.1f);
		}
		break;
	case 'H':
		{
			Globals::mainCamera->RotateUp(0.1f);
		}
		break;
	default:
		break;
	};
}

void CleanUp()
{
	glDeleteBuffers(1, &vboId);
}

int _tmain(int argc, _TCHAR* argv[])
{
	//identifying memory leaks
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); 

	ESContext esContext;

    esInitContext ( &esContext );

	esCreateWindow ( &esContext, "Hello Triangle", Globals::screenWidth, Globals::screenHeight, ES_WINDOW_RGB | ES_WINDOW_DEPTH);

	if ( Init ( &esContext ) != 0 )
		return 0;

	esRegisterDrawFunc ( &esContext, Draw );
	esRegisterUpdateFunc ( &esContext, Update );
	esRegisterKeyFunc ( &esContext, Key);

	esMainLoop ( &esContext );

	//releasing OpenGL resources
	CleanUp();

	printf("Press any key...\n");
	_getch();

	return 0;
}

